package com.example.application1.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.application1.API.festivalApi;
import com.example.application1.Adapter.festivalAdapter;
import com.example.application1.Entity.NewslistDTOXXXX;
import com.example.application1.Entity.festivalBean;
import com.example.application1.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class festivalFragment extends Fragment {

    private List<NewslistDTOXXXX> festivalList;
    private View view;
    private String date="2020-5-1";
    private EditText festivalEditText;
    private Button festivalSubmit;
    private RecyclerView FestivalRecycle;
    private festivalAdapter festivalAdapter1;

    public static festivalFragment newInstance() {
        Bundle args = new Bundle();
        festivalFragment fragment = new festivalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view=inflater.inflate(R.layout.festival_fragment,container,false);
      return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        requestFestivalData(date);
        festivalSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestFestivalData(festivalEditText.getText().toString());
            }
        });
        FestivalRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        festivalAdapter1=new festivalAdapter(getContext(),festivalList);
        FestivalRecycle.setAdapter(festivalAdapter1);
    }

    public void initView(){
        festivalEditText=view.findViewById(R.id.festivalEditText);
        festivalSubmit=view.findViewById(R.id.festivalSubmit);
        FestivalRecycle=view.findViewById(R.id.FestivalRecycle);

    }

    //网络数据请求方法
    private void requestFestivalData(String date) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        festivalApi service = retrofit.create(festivalApi.class);
        Call<festivalBean> call = service.getCall("1fe590b621dcb1472141d1dee8323c85",date);
        call.enqueue(new Callback<festivalBean>() {
            @Override
            public void onResponse(Call<festivalBean> call, Response<festivalBean> response) {
                festivalList=response.body().getNewslist();
                festivalAdapter1.refresh(festivalList);
            }
            @Override
            public void onFailure(Call<festivalBean> call, Throwable t) {
                System.out.println("连接失败");
                t.printStackTrace();
            }
        });
    }
}
